#include "SM3_controller.hpp"
void SM3_Controller::assign_count_plus()
{
	count_plus =  count.read() + 1 ;
}

void SM3_Controller::assign_ctrl()
{
	ctrl =  next_state.read() ;
}

void SM3_Controller::always_block1()

{
	

     			if(!rst.read())
	{

           
              current_state =  idle.read() ;
           	break;
        
	}
else
	{
           
              current_state =  next_state.read();
           	break;
     	break;
	}
	}
void SM3_Controller::always_block2()

{
	

     			if(!rst.read())
	{

              next_state =  idle.read() ;
        
	}
else
	{            switch(current_state.read() )
	{

                 idle:
                    			if(W.read()== 1)
	{

                         next_state =  write.read() ;
                      
	}
else			if(R.read()== 1)
	{

                         next_state =  read.read() ;
                      
	}
else
	{
                         next_state =  idle.read() ;
                    	break;
                  write:
                    			if(count.read() <count.read())
	{

                          next_state =  write.read() ;
                      
	}
else
	{
                          next_state =  encryption.read() ;
                   	break;
                 encryption:
                    			if(count.read() <count.read())
	{

                             next_state =  encryption.read() ;
                      
	}
else
	{			if(R.read()== 1)
	{

                                 next_state =  read.read() ;
                             
	}
else
	{
                                 next_state =  idle.read() ;
                    	break;
                read:
                    
                        next_state =  idle.read() ;
                    	break;


             endcase
       	break;
            


	}
	}
void SM3_Controller::always_block3()

{
	

    			if(!rst.read())
	{

         
           count =  case0;
           finish =  0 ;
           count_out =  case0;
         	break;
       
	}
else
	{
         
		   case(next_state)   
           idle:
			
				count_out  =  case0;
                finish =  0 ;
                count =  case0;
				break;
		   write:
	        
                   
				finish  =  0 ;
                count =  count_plus.read() ;
				count_out  =  count_plus.read();
            end	
           encryption:
            			if(count_out.read()<count_out.read())
	{

                      
                        finish =  0 ;
                        count =  count_plus.read() ;
						count_out  =  count_plus.read();
                      	break;
                     
					
	}
else
	{
                       
                         finish =  1 ;
                         count =  count_plus.read() ;
                         count_out =   case0 ;
                        	break;
						break;
           read:
                 
                     finish =  0;
                     count_out =  case0;
                     count =  case0;
                  	break;
/*	   default:
		
                  finish =  0;
		          count_out  =  case0;
                  count =  case0;
		end*/
		endcase	
	end	
  end				
						          
              
                     

          
      
	}
	}
